package GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLException;

import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import Other.Glob;

import database.DatabaseExtension;
import database.MyCountry;
import enums.SearchType;

public class SearchWindow extends JFrame {
	
	//singleton
	private static SearchWindow searchWindowInstance = null;
	
	private JPanel contentPane;
	private JTextField tfSearch;
	
	
	private JFrame thisFrame = this;

	
	boolean disable;
	
	/**
	 * Create the frame.
	 */
	private SearchWindow() {
		
		
		//System.out.println("This is the SearchType in the constructor of search Window" + Glob.type);
		setResizable(false);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 457, 120);
		setLocation(900,200);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		tfSearch = new JTextField();
		//set up Escape to Escape
		tfSearch.getInputMap(tfSearch.WHEN_IN_FOCUSED_WINDOW)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Search");
		tfSearch.getActionMap().put("Search", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				 
				 if (disable == false){
			escape();		
				 }
				
			    }
			
		});
		tfSearch.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				
		
					
	
			if(e.getKeyChar() == KeyEvent.VK_ENTER){
				
				
				searchReport();
													
					
			}

			}
		});
		tfSearch.setColumns(10);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent arg0) {
				
					//when button pressed
					searchReport();
					
					
				
			}
		});
		
		JButton btnExit = new JButton("Esc Exit");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (disable == false){
				//when button pressed
				escape();
				}
				
				
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(tfSearch, GroupLayout.PREFERRED_SIZE, 297, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(btnExit, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
						.addComponent(btnSearch, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(20)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(tfSearch, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnSearch))
					.addPreferredGap(ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
					.addComponent(btnExit))
		);
		contentPane.setLayout(gl_contentPane);
		
	}

//singleton
public static SearchWindow getInstance(){
		
		if(searchWindowInstance == null)
			
			searchWindowInstance = new SearchWindow();
			
		searchWindowInstance.invalidate();
		return searchWindowInstance;
		}


//get textfield from other screens
public String getFieldText() {
    return tfSearch.getText();
 }

//request focus for the textfield from other screens
public void setSearchBarFocus(){
	
	tfSearch.requestFocus();
}

//method to close the screen and 
public void escape(){
	
	SearchWindow s = SearchWindow.getInstance();
	s.dispose();
	
	
	
switch(Glob.type) {
	
	case COUNTRY:
		Country c = Country.getInstance();
		c.enableCountry();		
	break;
	
	
	case PAYMENT:
		Payment p = Payment.getInstance();
		p.enablePayment();	
	break;
	
	case INCOTERM:
		Incoterm i = Incoterm.getInstance();
		i.enableIncoterm();	
	break;
	
	case TAX:
		Tax t = Tax.getInstance();
		t.enableTax();	
	break;
	
	case ADDRESSEE:
		Addressee addressee = Addressee.getInstance();
		addressee.enableAddressee();	
	break;
	
	case ADDRESSEECOUNTRY:
		Addressee addresseeCountry = Addressee.getInstance();
		addresseeCountry.enableAddressee();	
	break;
	
	case SUPPLIER:
		Supplier supplier = Supplier.getInstance();
		supplier.enableSupplier();	
	break;
	
	case SUPPLIERCOUNTRY:
		Supplier supplierCountry = Supplier.getInstance();
		supplierCountry.enableSupplier();	
	break;
	
	case SUPPLIERPAYMENT:
		Supplier supplierPayment = Supplier.getInstance();
		supplierPayment.enableSupplier();	
	break;
	
	case SUPPLIERTAX:
		Supplier supplierTax = Supplier.getInstance();
		supplierTax.enableSupplier();	
	break;
	
	case CLIENT:
		Client client = Client.getInstance();
		client.enableClient();
	break;
	
	case CLIENTCOUNTRY:
		Client clientCountry = Client.getInstance();
		clientCountry.enableClient();
	break;
	
	case CLIENTPAYMENT:
		Client clientPayment = Client.getInstance();
		clientPayment.enableClient();
	break;
	
	case CLIENTTAX:
		Client clientTax = Client.getInstance();
		clientTax.enableClient();
	break;
	
	case DESTINATION:
		Destination destination = Destination.getInstance();
		destination.enableDestination();
	break;
	
	case DESTINATIONCOUNTRY:
		Destination destinationCountry = Destination.getInstance();
		destinationCountry.enableDestination();
	break;
	
	case SHIPMENT:
		ShipmentSearchOption shipment = ShipmentSearchOption.getInstance();
		shipment.enableShipmentSearchOption();
	break;
	
	case SHIPMENTCLIENT:
		Shipment shipmentClient = Shipment.getInstance();
		shipmentClient.enableShipment();
	break;
	
	case SHIPMENTADDRESSEE:
		Shipment shipmentAddressee = Shipment.getInstance();
		shipmentAddressee.enableShipment();
	break;
	
	case SHIPMENTDESTINATION:
		Shipment shipmentDestination = Shipment.getInstance();
		shipmentDestination.enableShipment();
	break;
	
	case SHIPMENTINCOTERM:
		Shipment shipmentIncoterm = Shipment.getInstance();
		shipmentIncoterm.enableShipment();
	break;
	
	case TRANSPORT:
		Transport transport = Transport.getInstance();
		transport.enableTransport();
	break;
	case TRANSPORTDESTINATION:
		Transport transportDestination = Transport.getInstance();
		transportDestination.enableTransport();
	break;
	case TRANSPORTSUPPLIER:
		Transport transportSupplier = Transport.getInstance();
		transportSupplier.enableTransport();
	break;
	case TRANSPORTCOUNTRY:
		Transport transportCountry = Transport.getInstance();
		transportCountry.enableTransport();
	break;
	case ADDSHIPMENTTRANSPORT:
		AddShipment addShipment = AddShipment.getInstance();
		addShipment.enableAddShipment();
	break;
	case ADDSHIPMENTSHIPMENT:
		ShipmentSearchOption addShipTran = ShipmentSearchOption.getInstance();
		addShipTran.enableShipmentSearchOption();
	break;
	
	//set up next type here
	}

	
	
}
public void disableSearchWindow(){
	
	SearchWindow.getInstance().disable();
	disable = true;
}

public void enableSearchWindow(){
	
	SearchWindow.getInstance().enable();
	disable = false;
}

public boolean getDisable(){
	
	return disable;
}

public void setTitle(){
	
switch(Glob.type) {
	
	case COUNTRY:
		setTitle("Country Search");
		break;
	
	case PAYMENT:
		setTitle("Payment Search");
		break;
		

	case INCOTERM:
		setTitle("Incoterm Search");
		break;
		
	case TAX:
		setTitle("Tax Search");
		break;
		
	case ADDRESSEE:
		setTitle("Addressee Search");
		break;
		
	case ADDRESSEECOUNTRY:
		setTitle("Country ID Search in Addressee");
		break;
		
	case SUPPLIER:
		setTitle("Supplier Search");
	break;
	
	case SUPPLIERCOUNTRY:
		setTitle("Country ID Search in Supplier");
		break;
		
	case SUPPLIERPAYMENT:
		setTitle("Payment ID Search in Supplier");
		break;
		
	case SUPPLIERTAX:
		setTitle("Tax ID Search in Supplier");
		break;
		
	case CLIENT:
		setTitle("Client Search");
	break;

	case CLIENTCOUNTRY:
		setTitle("Country ID Search in Client");
		break;
		
	case CLIENTPAYMENT:
		setTitle("Payment ID Search in Client");
		break;
		
	case CLIENTTAX:
		setTitle("Tax ID Search in Client");
		break;
		
		
	case DESTINATION:
		setTitle("Destination Search");
	break;

	case DESTINATIONCOUNTRY:
		setTitle("Country ID Search in Destination");
		break;
		
	case SHIPMENT:
		setTitle("Shipment Search");
		break;	
		
	case SHIPMENTCLIENT:
		setTitle("Client ID Search in Shipment");
		break;
		
	case SHIPMENTADDRESSEE:
		setTitle("Addressee ID Search in Shipment");
		break;
		
	case SHIPMENTDESTINATION:
		setTitle("Destination ID Search in Shipment");
		break;
	
	case SHIPMENTINCOTERM:
		setTitle("Incoterm ID Search in Shipment");
		break;
	case TRANSPORT:
		setTitle("Transport Search");
	break;
	case TRANSPORTDESTINATION:
		setTitle("Destination ID Search in Transport");
	break;
	case TRANSPORTSUPPLIER:
		setTitle("Supplier ID Search in Transport");
	break;
	case TRANSPORTCOUNTRY:
		setTitle("Country ID Search in Transport");
	break;
	case ADDSHIPMENTTRANSPORT:
		setTitle("Transport ID Search in Add Shipment");
	break;
	case ADDSHIPMENTSHIPMENT:
		setTitle("Shipment ID Search in Add Shipment");
	break;
		//set up next type here
}
	}
public void searchReport(){
	
	
	Report r =  new Report();
	r.setModel();
	SearchWindow s = SearchWindow.getInstance();
	s.disableSearchWindow();
	tfSearch.setText("");
	
	
	
	}
	
}
	
	
